package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.CloudInfo;
import jakarta.validation.constraints.NotNull;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CloudMapper {

    @Insert("INSERT INTO t_cloud_info (pid,cloud_base_path,cloud_name,cloud_desc,tenant_id,archive_status,create_time,update_time)" +
            " VALUES(#{pid},#{cloudBasePath},#{cloudName},#{cloudDesc},#{tenantId},#{archiveStatus},NOW(),NOW())")
    int saveCloud(CloudInfo cloudInfo);

    @Select("select * from t_cloud_info where pid=#{pid}")
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "cloud_base_path",property = "cloudBasePath"),
            @Result(column = "cloud_name",property = "cloudName"),
            @Result(column = "cloud_desc",property = "cloudDesc"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "archive_status",property = "archiveStatus")
    })
    CloudInfo queryCloudById(String pid);

    @Update({
            "<script>",
            " update t_cloud_info ",
            " <set> ",
            " update_time=NOW() ",
            "<if test='cloudInfo.cloudBasePath != null'>,cloud_base_path = #{cloudInfo.cloudBasePath}</if>",
            "<if test='cloudInfo.cloudName != null'>,cloud_name = #{cloudInfo.cloudName}</if>",
            "<if test='cloudInfo.cloudDesc != null'>,cloud_desc = #{cloudInfo.cloudDesc}</if>",
            "<if test='cloudInfo.tenantId != null'>,tenant_id = #{cloudInfo.tenantId}</if>",
            "<if test='cloudInfo.archiveStatus != null'>,archive_status = #{cloudInfo.archiveStatus}</if>",
            " </set> ",
            " where pid=#{pid} ",
            "</script>"
    })
    int updateCloudById(CloudInfo cloudInfo,String pid);

    @Select("Select count(0) ct from t_cloud_info where pid=#{cloudId} and archive_status='active'")
    long getActiveCloudCountById(String cloudId);

    @Select("Select * from t_cloud_info where pid=#{cloudId} and archive_status='active'")
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "cloud_base_path",property = "cloudBasePath"),
            @Result(column = "cloud_name",property = "cloudName"),
            @Result(column = "cloud_desc",property = "cloudDesc"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "archive_status",property = "archiveStatus")
    })
    CloudInfo getActiveCloudById(String cloudId);

    @Select({"<script>",
            "Select * from t_cloud_info WHERE tenant_id=#{tenantId} ",
            "<if test='cloudName != null'> AND cloud_name = #{cloudName}</if>",
            "<if test='archiveStatus != null'> AND archive_status = #{archiveStatus}</if>",
            "<if test='cloudBasePath != null'> AND cloud_base_path = #{cloudBasePath}</if>",
            " limit #{pageSize} offset #{startRow}",
            "</script>"})
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "cloud_base_path",property = "cloudBasePath"),
            @Result(column = "cloud_name",property = "cloudName"),
            @Result(column = "cloud_desc",property = "cloudDesc"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "archive_status",property = "archiveStatus")
    })
    List<CloudInfo> queryCloudByTenantId(int startRow, Integer pageSize, String tenantId, String cloudName,String cloudBasePath,String archiveStatus);

    @Select({"<script>",
            "Select count(0) from t_cloud_info WHERE tenant_id=#{tenantId} ",
            "<if test='cloudName != null'> AND cloud_name = #{cloudName}</if>",
            "<if test='archiveStatus != null'> AND archive_status = #{archiveStatus}</if>",
            "<if test='cloudBasePath != null'> AND cloud_base_path = #{cloudBasePath}</if>",
            "</script>"})
    long getCloudRecordNum(String tenantId, String cloudName, String cloudBasePath, String archiveStatus);
    @Select({"<script>",
            "Select count(0) from t_cloud_info WHERE pid!=#{pid} ",
            "<if test='cloudName != null'> AND cloud_name = #{cloudName}</if>",
            "<if test='archiveStatus != null'> AND archive_status = #{archiveStatus}</if>",
            "<if test='cloudBasePath != null'> AND cloud_base_path = #{cloudBasePath}</if>",
            "</script>"})
    long getCloudCountByNotId(@NotNull String pid, String cloudName, String cloudBasePath, String archiveStatus);
}
